package org.example.product.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.entity.Product;
import org.example.product.mapper.ProductMapper;
import org.example.product.service.ProductService;

import org.example.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fanfutian
 * @since 2025-04-23
 */
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public Product getProductById(Long productId) {
        log.info("查询数据库，查询商品信息，商品id:{}", productId);
        Product product = productMapper.selectProductById(productId);
        if (product == null) {
            log.error("商品不存在，商品id:{}", productId);
            return null;
        }
        log.info("数据："+product.toString());
        return product;
    }

    @Override
    public List<ProductVo> getAllProduct() {
        log.info("查询数据库，获取所有商品信息");
        List<Product> product = productMapper.selectProductAll();
        if(product == null){
            log.error("数据库中没有任何商品信息");
            return null;
        }
        List<ProductVo> productVo = new ArrayList<>();
        for (Product p : product){
            ProductVo newProductVo = new ProductVo();
            BeanUtils.copyProperties(p, newProductVo);
            productVo.add(newProductVo);
        }
        return productVo;
    }

    @Override
    public void updateProduct(Product product) {
        log.info("修改商品信息中到的不为null的字段：{}",product);
        productMapper.updateProduct(product);
    }

}
